const http = require("http");
const fs = require("fs");
const url = require("url");
const qs = require("querystring");

http.createServer((req, res)=>{
    if(req.url !== "/favicon.ico"){
    console.log(req.url)
        // 先规划，将来会过来哪些类请求
        // 文件请求
        // http://localhost:3000/taobao/html/index.html
        // 数据请求
        // http://localhost:3000/api?type=login&username=admin&password=123123
        // http://localhost:3000/api?type=register&username=admin&password=123123

        // 路由处理
        if(req.url.indexOf("/api") != -1){
            // 数据请求

            message(req, res);
        }else{
            // 文件请求
            file(req, res)
        }
    }
}).listen(3000,()=>{
    console.log("服务器已开启")
});

function message(req, res){
    // 数据请求处理
    

    let str = "";
    req.on("data",(c)=>{
        str += c;
    });
    req.on("end",()=>{
        let dataObj;
        if(str){
            dataObj = qs.parse(str);
        }else{
            dataObj = url.parse(req.url, true).query;
        }
        // console.log(dataObj);
        if(dataObj.type === "login"){
            login(dataObj,req,res)
        }
        if(dataObj.type === "register"){
            register(dataObj,req,res)
        }
        // res.end()
    })
}

function login(data,req,res){

    fs.readFile("./data/user.json",(err,w)=>{
        let userMsg = JSON.parse(w);
    
    
        let i=0;
        const flag = userMsg.some((val,idx)=>{
            i = idx;
            return val.username === data.username;
        })
        console.log(flag,i,userMsg,data)
        if(flag){
            if(userMsg[i].password === data.password){
       
                res.write(JSON.stringify({code:2,title:"登录成功",data:data.username}))
            }else{
             
                res.write(JSON.stringify({code:1,title:"密码不对",data:"NOTDATA"}))
            }
        }else{
            
            res.write(JSON.stringify({code:0,title:"用户名不存在",data:"NOTDATA"}))
        }
        res.end();
    })
}

function register(data,req,res){

    fs.readFile("./data/user.json",(err,w)=>{
        let userMsg = JSON.parse(w);
        console.log(userMsg)
        if(userMsg.some(val=>val.username === data.username)){
            res.write(JSON.stringify({code:0,title:"用户名重复",data:"NOTDATA"}))
            res.end();

        }else{
            userMsg.push({
                username: data.username,
                password: data.password
            })
            res.write(JSON.stringify({code:1,title:"注册成功",data:data.username}))
            console.log(userMsg)
            fs.writeFile("./data/user.json",JSON.stringify(userMsg),err=>{
                if(err){
                    res.write(JSON.stringify({code:2,title:"注册失败",data:"NOTDATA"}))
                }
                res.end();
            })

        }
    })
}

function file(req, res){
    var q = url.parse(req.url).pathname
    fs.readFile("." + q,(err, data)=>{
        if(err){
            res.write("404")
        }else{
            res.write(data)
        }
        res.end();
    })
}